package cn.lhjl.day08.JDBC.JdbeDemo07;

import cn.lhjl.day08.Utils.JdbcUtils;

import javax.swing.text.Utilities;
import java.sql.*;
import java.util.ArrayList;

//把rs结果集的数据封装成一一个个对象，再把对象放在一个集合中返回

public class Method1 {
    public static void main(String[] args) {
        ArrayList<Student> list = new Method1().method();
        System.out.println(list);
    }
    public ArrayList<Student> method() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<Student> list = new ArrayList<>();
        try {
            conn = JdbcUtils.getConnect();
            stmt = conn.createStatement();
            String sql = "select * from student";
            rs = stmt.executeQuery(sql);
            //会重复用到的变量最好在循环体类修改
            Student student =null;
            while (rs.next()){
                int sid = rs.getInt("sid");
                String sname = rs.getString("sname");
                int sage = rs.getInt("sage");
                String ssex = rs.getString("ssex");
                String snativeplace = rs.getString("snativeplace");
                String smajor = rs.getString("smajor");
                String sclass = rs.getString("sclass");
                String snative = rs.getString("snative");
                student = new Student();
                student.setsId(sid);
                student.setsName(sname);
                student.setsSex(ssex);
                student.setsNativePlace(snativeplace);
                student.setsMajor(smajor);
                student.setsClass(sclass);
                student.setsNative(snative);
                list.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(rs,stmt,conn);
        }
        return list;
    }
}
